package com.feifan.monthhelper.ui.base;

import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.feifan.monthhelper.R;
import com.feifan.monthhelper.ui.dialog.LoadingDialog;
import com.feifan.monthhelper.utils.ToastHelper;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseActivity extends AppCompatActivity {
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    protected LoadingDialog loadingDialog;
    protected LoadingDialog.Builder loadBuilder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        setContentView(setContentLayout());
        ButterKnife.bind(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initStatusBarAndTitleView();

        initData();
        initView();
    }

    private void initStatusBarAndTitleView() {
//        initStatusBar();

//        if(getTitleView() != null) {
//            BarUtils.setStatusBarLightMode(this, false);
//            BarUtils.addMarginTopEqualStatusBarHeight(getTitleView());
//        }
    }

    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
                getWindow().setNavigationBarColor(getResources().getColor(R.color.bg_color_navigation_bar));
            }
            /* API6.0 以上 支持状态栏深色文字颜色 */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        }
    }

    public void showToast(int resId) {
        showToast(getResources().getString(resId));
    }

    public void showToast(String toastStr) {
        ToastHelper.showShort(getApplication(), toastStr);
    }

    public View getTitleView() {
        return null;
    }

    protected abstract void initData();

    protected abstract void initView();

    protected abstract int setContentLayout();

    public void addDisposable(Disposable disposable) {
        compositeDisposable.add(disposable);
    }

    public void showLoadingDialog(String msg) {
        if (loadingDialog == null) {
            loadBuilder = new LoadingDialog.Builder(this)
                    .setMessage(msg)
                    .setCancelable(true)
                    .setCancelOutside(false);
            loadingDialog = loadBuilder.create();
            loadingDialog.show();
        } else {
            if (!loadingDialog.isShowing()) {
                loadingDialog.show();
            }
        }
    }

    public void dismisDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.cancel();loadingDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.dispose();
    }
}
